
package session.stateless;


import entity.AccessControl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ejb.Stateless;
import java.util.ArrayList;
import entity.AccessRole;
import javax.ejb.EJB;

@Stateless
public class AccessControlSessionBean implements AccessControlSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;
    
    @EJB
    private AccessRoleSessionBeanLocal accessRoleSessionBeanLocal;
      
    public void addControl (String title, String pageAddress, String description, String department, String header, String relatedPage){
        
        
        Query query = entityManager.createQuery("SELECT u FROM AccessControl u WHERE u.title = :intitle");
        query.setParameter("intitle", title);
        AccessControl accessControl = null;

        try {
            accessControl = (AccessControl) query.getSingleResult();
        } catch (Exception ex) {
        }
        
        if (accessControl == null)
        {
            if (relatedPage.length()==0)
                relatedPage = null;
            accessControl = new AccessControl (title, pageAddress, description, department, header, relatedPage);
            entityManager.persist(accessControl);
            entityManager.flush();
        }
        
    }
    @Override
    public ArrayList <AccessControl> getAccessControls ()
    {
        Query query = entityManager.createQuery("SELECT u FROM AccessControl u");
        ArrayList<AccessControl> cs = new ArrayList<AccessControl>();
        for (Object o : query.getResultList()) {
            AccessControl c = (AccessControl) o;
            cs.add(c);
        }
        return cs;
    }
    
    @Override
    public AccessControl getControl (Long controlId)
    {
  
        AccessControl accessControl = entityManager.find(AccessControl.class, controlId);
        return accessControl;
    }
    
    @Override
    public void updateControl (Long controlId, String title, String pageAddress, String description, String department, String header, String relatedPage)
    {
      
        AccessControl accessControl = entityManager.find(AccessControl.class, controlId);
        if (accessControl != null)
        {
               accessControl.setTitle(title);
               accessControl.setDescription(description);
               accessControl.setPageAddress(pageAddress);
               accessControl.setDepartment(department);
               accessControl.setHeader(header);
               accessControl.setRelatedpage(relatedPage);
               entityManager.persist(accessControl);
               entityManager.flush();
        }
    }   
    
    @Override
    public AccessControl getControl (String title)
    {
        Query query = entityManager.createQuery("SELECT u FROM AccessControl u WHERE u.title = :intitle");
        query.setParameter("intitle", title);
        AccessControl accessControl = null;

        try {
            accessControl = (AccessControl) query.getSingleResult();
        } catch (Exception ex) {
        }
        return accessControl;
    }
    
    @Override
    public void deleteControl (Long controlId)
    {
        
        AccessControl accessControl = getControl (controlId);
         if (accessControl != null)
         {    
                
                for (AccessRole e: accessRoleSessionBeanLocal.getAccessRoles())
                {
                    if (e.getAccessControls().contains(accessControl))
                    {
                        e.getAccessControls().remove(accessControl);        
                            entityManager.persist(e);
                            entityManager.flush();
                        }
                    }
                
                
                entityManager.remove(accessControl);
         }
    }
    
    }
